/**
 * 复制模板
 * 格式：node mktpl.mjs {project-name} {module-name}
 * 示例：node mktpl.mjs joyhabit 习惯
 */
import fs from 'fs';

const args = process.argv.slice(2);
if (args.length != 2) {
    console.error('error: node mktpl.mjs miss argument.\neg. node mktpl.mjs {project-name} {module-name}');
    process.exit();
}

const [projectName, moduleName] = args;

const dest = `../markdown/${projectName}/${moduleName}`;
fs.cpSync('./markdown', dest, { recursive: true, force: false });
fs.cpSync('./public', `../public/static/${projectName}/${moduleName}`, { recursive: true, force: false });

if (true) {
    let content = '', filePath = '', dateStr = (new Date()).toLocaleString('zh-cn', { year: 'numeric', month: '2-digit', day: '2-digit' }).replaceAll('/', '-');
    fs.readdirSync(dest).forEach(file => {
        if (file.endsWith('.md')) {
            filePath = `${dest}/${file}`;
            content = fs.readFileSync(filePath, 'utf-8');
            content = content.replaceAll('__date__', dateStr);
            content = content.replaceAll('__project__', projectName);
            content = content.replaceAll('__module__', moduleName);
            fs.writeFileSync(filePath, content);
        }
    })
}

console.log(`复制模板成功：${projectName}/${moduleName}`);